package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.activity.product.DetailProductActivity;
import com.avic.rong.userOrder.bean.OrderDetailsBean;
import com.avic.rong.utils.GlideUtils;
import com.bumptech.glide.Glide;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

/**
 * Created by XinEr on 2019/7/29.
 * Describe:
 */
public class OrderGoodsWeiFaHuoListAdapter extends RecyclerView.Adapter<OrderGoodsWeiFaHuoListAdapter.ViewHolder> {
    private Context context;
    private int type = 1;
    private List<OrderDetailsBean.ItemListBean> itemList;
    private OrderDetailsBean orderDetailsBean;


    public OrderGoodsWeiFaHuoListAdapter(Context context, List<OrderDetailsBean.ItemListBean> itemList) {
        this.context = context;
        this.itemList = itemList;

    }

    public OrderDetailsBean getOrderDetailsBean() {
        return orderDetailsBean;
    }

    public void setOrderDetailsBean(OrderDetailsBean orderDetailsBean) {
        this.orderDetailsBean = orderDetailsBean;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.order_goods_details_item, viewGroup, false);
        return new ViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        OrderDetailsBean.ItemListBean itemListBean = itemList.get(i);
        if (itemListBean.getSendNumber() < itemListBean.getGoodsNumber()) {
            OrderDetailsBean.ItemListBean.GoodBeanX good = itemListBean.getGood();
//            GlideUtils.loaderImage(context,good.getGoodsImg()+"",viewHolder.iv_photo);
            viewHolder.tv_title.setText(good.getGoodsName());

            if (!TextUtils.isEmpty(good.getAttrShow())) {
                viewHolder.tv_status.setText(good.getAttrShow());
                viewHolder.tv_status.setVisibility(View.VISIBLE);
            }


            JSONArray jsonArray = null;
            String skuName = "";
            try {
                if (TextUtils.isEmpty(good.getSkuName())) {
                    return;
                }
                jsonArray = new JSONArray(good.getSkuName());
                StringBuffer paramsSb = new StringBuffer("");
                StringBuffer moreParamsb = new StringBuffer("");
                for (i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                    Iterator<String> keys = jsonObject.keys();
                    String key = "";
                    while (keys.hasNext()) {//遍历JSONObject

                        key = (String) keys.next().toString();

                        if (null != key && !"".equals(key)) {

                            String value = (String) jsonObject.get(key);
                            if (jsonArray.length() > 1) {
//                            skuName = value +"; ";
                                paramsSb.append(value + " ; ");
                            } else {
                                paramsSb.append(value);
                            }

                        }

                    }
                }
                if (jsonArray.length()>1){
                    moreParamsb.append(paramsSb.deleteCharAt(paramsSb.length() - 2));
                    viewHolder.tv_time.setText(moreParamsb);
                }else {
                    viewHolder.tv_time.setText(paramsSb.toString());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


//            viewHolder.tv_time.setText(good.getSkuName());
            viewHolder.tv_price.setText("¥" + good.getShopPrice() + "");
            viewHolder.tv_order_number.setText("x" + (itemListBean.getGoodsNumber() - itemListBean.getSendNumber()));
            if (orderDetailsBean != null) {


                if (orderDetailsBean.getOrder().getOrderType() == 4) {
                    if (!TextUtils.isEmpty(good.getGoodsImg())) {
                        if (good.getGoodsImg().contains("rongrong.")) {
                            GlideUtils.loaderImage(context, good.getGoodsImg(), viewHolder.iv_photo);
                        } else {
                            String biaoZhunImg = orderDetailsBean.getBiaoZhunImg();
                            GlideUtils.loaderImage(context, biaoZhunImg + good.getGoodsImg(), viewHolder.iv_photo);
                        }
                        viewHolder.iv_photo.setVisibility(View.VISIBLE);
                        viewHolder.ll_pic_tag.setVisibility(View.GONE);
                    } else {
                        String keywords = itemListBean.getProModel();
                        String catName = good.getCatName();
                        viewHolder.tv_goods_type.setText(catName);
                        viewHolder.tv_goods_tel.setText(keywords);
                        viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
                        viewHolder.iv_photo.setVisibility(View.VISIBLE);
                        Glide.with(context).load(R.mipmap.common_bg).into(viewHolder.iv_photo);
                    }
                } else {
                    if (!TextUtils.isEmpty(good.getGoodsImg())) {
                        GlideUtils.loaderImage(context, good.getGoodsImg(), viewHolder.iv_photo);
                        viewHolder.iv_photo.setVisibility(View.VISIBLE);
                        viewHolder.ll_pic_tag.setVisibility(View.GONE);
                    } else {
                        //默认图
//                GlideUtils.loaderImage(context, R.mipmap.common_bg, iv_goods_pic);
                        String keywords = itemListBean.getProModel();
                        String catName = good.getCatName();
                        viewHolder.tv_goods_type.setText(catName);
                        viewHolder.tv_goods_tel.setText(keywords);
                        Glide.with(context).load(R.mipmap.common_bg).into(viewHolder.iv_photo);
                        viewHolder.iv_photo.setVisibility(View.VISIBLE);
                        viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
                    }

                }
            }

        }
        viewHolder.ll_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(itemListBean.getSkuId() + "")) return;
                context.startActivity(DetailProductActivity.getIntent(context, itemListBean.getSkuId() + ""));
            }
        });

        if (itemList.size()-1==i){
            viewHolder.line_view.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_photo;
        private TextView tv_title;
        private TextView tv_time;
        private TextView tv_price;
        private TextView tv_order_number;
        private LinearLayout ll_root;
        private TextView tv_status;
        private TextView tv_goods_type;
        private TextView tv_goods_tel;
        private LinearLayout ll_pic_tag;
        private View line_view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_photo = itemView.findViewById(R.id.iv_photo);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_order_number = itemView.findViewById(R.id.tv_order_number);
            ll_root = itemView.findViewById(R.id.ll_root);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_goods_type = itemView.findViewById(R.id.tv_goods_type);
            tv_goods_tel = itemView.findViewById(R.id.tv_goods_tel);
            ll_pic_tag = itemView.findViewById(R.id.ll_pic_tag);
            line_view = itemView.findViewById(R.id.line_view);
        }
    }
}
